using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ERP.ERPSystem.WarehouseManagement;
using ERP.ERPSystem.WarehouseManagement.Dto;
using Microsoft.AspNetCore.Mvc;

namespace ERP.Web.Controllers.WarehouseManagement
{
    /// <summary>
    /// 供应商退货控制器
    /// </summary>
    public class SupplierReturnsController : Controller
    {
        private readonly ISupplierReturnsServices _supplierReturnsServices;

        public SupplierReturnsController(ISupplierReturnsServices supplierReturnsServices)
        {
            _supplierReturnsServices = supplierReturnsServices;
        }

        /// <summary>
        /// 供应商退货列表页面
        /// </summary>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 供应商退货表单页面
        /// </summary>
        public IActionResult Form()
        {
            return View();
        }

        /// <summary>
        /// 供应商选择页面
        /// </summary>
        public IActionResult SupplierSelect()
        {
            return View();
        }

        /// <summary>
        /// 获取供应商分页列表
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> GetSuppliers([FromForm] string keyword, [FromForm] int pageNumber = 1, [FromForm] int pageSize = 10)
        {
            try
            {
                var result = await _supplierReturnsServices.GetSuppliersAsync(keyword, pageNumber, pageSize);
                return Json(new 
                { 
                    success = result.Success,
                    message = result.Message,
                    data = result.Data
                });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "查询供应商失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 根据供应商编码获取供应商信息
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> GetSupplierByCode(string supplierCode)
        {
            try
            {
                var result = await _supplierReturnsServices.GetSupplierByCodeAsync(supplierCode);
                return Json(new 
                { 
                    success = result.Success,
                    message = result.Message,
                    data = result.Data
                });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "查询供应商信息失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 获取供应商退货分页列表
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> GetPagedList([FromForm] SupplierReturnsQueryDto query)
        {
            try
            {
                var result = await _supplierReturnsServices.GetPagedListAsync(query);
                return Json(new 
                { 
                    success = result.Success,
                    message = result.Message,
                    data = result.Data
                });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "查询失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 根据ID获取供应商退货详情
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> GetById(int id)
        {
            try
            {
                var result = await _supplierReturnsServices.GetByIdAsync(id);
                return Json(new 
                { 
                    success = result.Success,
                    message = result.Message,
                    data = result.Data
                });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "获取详情失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 创建供应商退货
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> Create([FromForm] SupplierReturnsCreateDto createDto)
        {
            try
            {
                var result = await _supplierReturnsServices.CreateAsync(createDto);
                return Json(new 
                { 
                    success = result.Success,
                    message = result.Message,
                    data = result.Data
                });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "创建失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 更新供应商退货
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> Update([FromForm] SupplierReturnsUpdateDto updateDto)
        {
            try
            {
                var result = await _supplierReturnsServices.UpdateAsync(updateDto);
                return Json(new 
                { 
                    success = result.Success,
                    message = result.Message,
                    data = result.Data
                });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "更新失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 删除供应商退货
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> Delete(int id)
        {
            try
            {
                var result = await _supplierReturnsServices.DeleteAsync(id);
                return Json(new 
                { 
                    success = result.Success,
                    message = result.Message
                });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "删除失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 批量删除供应商退货
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> BatchDelete([FromForm] List<int> ids)
        {
            try
            {
                var result = await _supplierReturnsServices.BatchDeleteAsync(ids);
                return Json(new 
                { 
                    success = result.Success,
                    message = result.Message
                });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "批量删除失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 获取物料产品分页列表
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> GetMaterialProducts([FromForm] string keyword, [FromForm] int pageNumber = 1, [FromForm] int pageSize = 10)
        {
            try
            {
                var result = await _supplierReturnsServices.GetMaterialProductsAsync(keyword, pageNumber, pageSize);
                return Json(new 
                { 
                    success = result.Success,
                    message = result.Message,
                    data = result.Data
                });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = "查询物料产品失败：" + ex.Message });
            }
        }
    }
} 